package com.enjoyor.soa.traffic.rest.efoms.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.enjoyor.filter.SSOClientfilter;
import com.enjoyor.soa.traffic.api.uums.dto.UserDto;
import com.enjoyor.soa.traffic.api.uums2.service.dubbo.DubboTokenService;
import com.enjoyor.soa.traffic.frame.annotation.Auth;
import com.enjoyor.soa.traffic.rest.efoms.dubbo.invoke.DubboUumsServiceInvoke;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Auth
@RestController
@RequestMapping({ "/system" })
@Api("SystemController")
@CrossOrigin
@Component
public class SystemController {
	
	@Autowired
	private DubboUumsServiceInvoke dubboUumsServiceInvoke;
	
	@GetMapping(value = { "/logout" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
    @ApiOperation("系统退出")
	@ApiImplicitParams({
    	@ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String") })
	public Boolean logout(@RequestParam(defaultValue = "",value="token",required=false) String devId,HttpServletRequest request, HttpServletResponse response) {
	    String token = (String) request.getAttribute("token");//获取token；
	    UserDto userDto = SSOClientfilter.getUserInfo(token);//根据token匹配用户信息；
	    String const_systemKey = userDto.getSystemKey();//获取用户信息中的systemKey；
	    request.getSession().invalidate();//清楚repuset作用域的session；
	    ResultPojo pojo = dubboUumsServiceInvoke.getResultPojo(token, const_systemKey);//用户信息清楚；
	    if (pojo != null && pojo.getAppCode().compareTo("0") == 0) {
	      return true;
	    }
	    return false; 
	}
}
